﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TransCend.Model;

namespace TransCend.DAL
{
    public class ModelProperty
    {
        private List<Column> columns = new List<Column>();
        private Dictionary<string, Column> columnMAP = new Dictionary<string, Column>();
        private Column pkcolumn = null;
        private PrimaryKeyAttribute primaryType = null;
        private string tableName = null;

        public void AddColumn(Column c)
        {
            columns.Add(c);
            columnMAP[c.Name] = c;
        }

        public void SetPrimaryKey(PrimaryKeyAttribute pk)
        {
            primaryType = pk;
        }

        public void SetTableName(string tn)
        {
            tableName = tn;
        }

        public void SetPkColumn(Column c)
        {
            pkcolumn = c;
        }

        public Column GetPkColumn()
        {
            return pkcolumn;
        }

        public PrimaryKeyAttribute GetPrimaryKey()
        {
            return primaryType;
        }

        public string GetPk()
        {
            return pkcolumn != null ? pkcolumn.Name : null;
        }

        public String GetTableName()
        {
            return tableName;
        }

        public List<Column> GetUpdateColumns()
        {
            List<Column> clist = new List<Column>();
            foreach (Column c in columns)
            {
                if (c.Updatable && !(c.Name == pkcolumn.Name))
                {
                    clist.Add(c);
                }
            }
            return clist;
        }

        public List<Column> GetInsertColumns()
        {
            List<Column> clist = new List<Column>();
            foreach (Column c in columns)
            {
                if (c.Insertable
                        && !(c.Name == pkcolumn.Name && primaryType.Identity))
                {
                    clist.Add(c);
                }
            }
            return clist;
        }

        public List<Column> GetColumns()
        {
            return columns;
        }

        public Column GetColumn(string name)
        {
            return columnMAP[name];
        }
    }
}
